#include "gtest/gtest.h"
#include "App.h"
#include "Util.h"

TEST(AppTest, main){
    int argc = 0;
    char * argv[] = {
        (char*)"-h",
    };

    App app;
    app.run(argc, argv);
}

TEST(FileTest, isExist){
    const char * absentPath = "nofile_isExist";
    ASSERT_FALSE(util::File::isExist(absentPath));

    const char * makeFilePath = "Makefile";
    ASSERT_TRUE(util::File::isExist(makeFilePath));
}

TEST(FileTest, createIfAbsent){
    const char * path = "nofile_createIfAbsent";
    ASSERT_EQ(0, util::File::createIfAbsent(path));
    ASSERT_TRUE(util::File::isExist(path));

    ASSERT_EQ(0, util::File::remove(path));
    ASSERT_FALSE(util::File::isExist(path));
}
TEST(FileTest, remove){
    const char * path = "nofile_remove";
    ASSERT_EQ(0, util::File::createIfAbsent(path));

    ASSERT_EQ(0, util::File::remove(path));
    ASSERT_FALSE(util::File::isExist(path));
}

#include "BreakPointService.h"
TEST(BreakPointServiceTest, use){
    BreakPointService bpService;

    bpService.start();
    bpService.connected();
    bpService.receivedLoginResp("loginrsp-0-OK");

    bpService.timeout(); 
    bpService.receivedBreakPointResp("breakpoint-0-OK");

    bpService.receivedNormalResp("normal_1-0-OK");
    bpService.receivedNormalResp("normal_2-0-OK");
    bpService.receivedNormalResp("normal_3-0-OK");

    bpService.disconnected();
    
    bpService.stop();
}



#include "BreakPointMem.h"
#include "BreakPointFile.h"

TEST(BreakPointMemTest, use){
    IBreakPoint * bp = new BreakPointMem();
    ASSERT_EQ(-1, bp->get());

    const long long defSeqNo = 10001;
    bp->set(defSeqNo);
    ASSERT_EQ(defSeqNo, bp->get());

    bp->add(1);
    ASSERT_EQ(defSeqNo + 1, bp->get());

    delete bp;
}

TEST(BreakPointFileTest, use){

    const char * path = "seqNo.bin";
    util::File::remove(path);
    IBreakPoint * bp = new BreakPointFile(path);

    const long long defSeqNo = 10001;
    bp->set(defSeqNo);
    ASSERT_EQ(defSeqNo, bp->get());

    bp->add(1);
    ASSERT_EQ(defSeqNo + 1, bp->get());

    delete bp;
}


#include "BreakPointDecorator.h"
TEST(BreakPointDecoratorTest, use){

    const char * path = "seqNo.bin";
    util::File::remove(path);

    IBreakPoint * bp = new BreakPointDecorator(new BreakPointFile(path));

    const long long defSeqNo = 10001;
    bp->set(defSeqNo);
    ASSERT_EQ(defSeqNo, bp->get());

    bp->add(1);
    ASSERT_EQ(defSeqNo + 1, bp->get());

    delete bp;
}


#include "FileStreamCharPersist.h"

TEST(FileStreamCharPersistTest, use){
    const char * path = "charPersist.bin";
    util::File::remove(path);

    IPersist * pst = new FileStreamCharPersist(path);

    const long long defSeqNo = 10001;
    pst->open();
    ASSERT_EQ(defSeqNo, pst->readLongLong());
    pst->close();

    pst->open();
    const long long val = 2345;
    pst->writeLongLong(val);
    pst->flush();
    pst->close();

    pst->open();
    ASSERT_EQ(val, pst->readLongLong());
    pst->close();
    delete pst;
}


#include "FileStreamBinaryPersist.h"

TEST(FileStreamBinaryPersistTest, use){
    const char * path = "binaryPersist.bin";
    util::File::remove(path);
    IPersist * pst = new FileStreamBinaryPersist(path);

    const long long defSeqNo = 10001;
    pst->open();
    ASSERT_EQ(defSeqNo, pst->readLongLong());
    pst->close();

    pst->open();
    const long long val = 2345;
    pst->writeLongLong(val);
    pst->flush();
    pst->close();

    pst->open();
    ASSERT_EQ(val, pst->readLongLong());
    pst->close();
    delete pst;
}
